'use strict';

const Controller = require('egg').Controller;
let jwt = require("jsonwebtoken")

class HomeController extends Controller {

    //渲染房源列表数据
    async housingList() {
        const { ctx, app } = this
        const data = await app.mysql.select('housingList')
        const { authorization } = ctx.request.header

        try {
            let res = jwt.verify(authorization, '1901A')
            ctx.body = {
                code: 1,
                msg: "成功",
                res,
                data
            }
        } catch (error) {
            ctx.body = {
                code: 0,
                msg: '登录失效，请重新登录'
            }
        }
    }



    //房源管理删除数据
    async tableDataDel() {
        const { app, ctx } = this
        const { id } = ctx.request.body
        const data = await app.mysql.delete('housingList', { id })

        ctx.body = {
            code: 1,
            data
        }
    }

    //房源管理添加数据
    async housingAdd() {
        const { app, ctx } = this
        const { name, area, price, time, sort } = ctx.request.body
        console.log(ctx.request.body);
        
        const data = await app.mysql.insert(`housingList`,{name, area, price, time, sort})

        ctx.body = {
            code: 1,
            data
        }
    }




    //房源管理修改数据
    async housingEdit() {
        const { app, ctx } = this
        const { name, area, price, time, sort, id } = ctx.request.body
        const data = await app.mysql.update('housingList', { id, name, area, price, time, sort })

        ctx.body = {
            code: 1,
            data
        }
    }


    //渲染团购列表数据
    async tableData2() {
        const { ctx, app } = this
        const data = await app.mysql.select('tableData2')
        const { authorization } = ctx.request.header
        try {
            let res = jwt.verify(authorization, '1901A')
            ctx.body = {
                code: 1,
                msg: "成功",
                res,
                data
            }
        } catch (error) {
            ctx.body = {
                code: 0,
                msg: '登录失效，请重新登录'
            }
        }
    }



    //渲染直播列表数据
    async liveList() {
        const { ctx, app } = this
        const data = await app.mysql.select('liveList')
        const { authorization } = ctx.request.header
        try {
            let res = jwt.verify(authorization, '1901A')
            ctx.body = {
                code: 1,
                msg: "成功",
                res,
                data
            }
        } catch (error) {
            ctx.body = {
                code: 0,
                msg: '登录失效，请重新登录'
            }
        }
    }


    //直播类型筛选
    async livetype() {
        const { app, ctx } = this
        const { value1 } = ctx.request.body
        const data = await app.mysql.select("liveList", { where: { type: value1 } })
        if (value1 === '请选择') {
            ctx.body = {
                code: 1,
                data
            }
        } else {
            ctx.body = {
                code: 0,
                data
            }
        }
    }


    //团购类型筛选
    async tgtype() {
        const { app, ctx } = this
        const { value1 } = ctx.request.body
        const data = await app.mysql.select("tableData2", { where: { state: value1 } })

        if (value1 === '请选择') {
            ctx.body = {
                code: 1,
                data
            }
        } else {
            ctx.body = {
                code: 0,
                data
            }
        }
    }



    //直播搜索
    async livesearch() {
        const { app, ctx } = this
        const { value } = ctx.request.body
        const data = await app.mysql.query(`select * from liveList where title like "%${value}%"`)

        ctx.body = {
            code: 1,
            data
        }
    }

    //团购搜索
    async tgsearch() {
        const { app, ctx } = this
        const { value } = ctx.request.body
        const data = await app.mysql.query(`select * from tableData2 where name like "%${value}%"`)

        ctx.body = {
            code: 1,
            data
        }
    }


    //房源搜索
    async roomsearch() {
        const { app, ctx } = this
        const { value } = ctx.request.body
        const data = await app.mysql.query(`select * from housingList where name like "%${value}%"`)

        ctx.body = {
            code: 1,
            data
        }
    }

    //直播详情数据
    async Detail() {
        const { app, ctx } = this
        const { id } = ctx.request.body
        const data = await app.mysql.get('liveList', { id })

        ctx.body = {
            code: 1,
            data
        }
    }


    //团购管理删除数据
    async tableDataDel2() {
        const { app, ctx } = this
        const { id } = ctx.request.body
        const data = await app.mysql.delete('tableData2', { id })

        ctx.body = {
            code: 1,
            data
        }
    }

    async three() {
        const { ctx, app } = this
        const { value } = ctx.request.body
        console.log(value);
        const data = await app.mysql.query(`select * from housinglist where area like "%${value.name}%"`)
        ctx.body = {
            data,
            code: '000'
        }

    }

}

module.exports = HomeController;
